package com.buobao.lambda;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/**
 * 通过predicate来实现函数式编程
 * 用于条件判断固定返回布尔值
 */
public class PredicateSample {
    public static void main(String[] args) {
        Predicate<Integer> predicate = n->n>4;
        System.out.println(predicate.test(3));
        List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9);
        //查找奇数
        filter(list,n->n%2==1);
        filter(list,n->n%2==0);
        filter(list,n->n>5&&n%2==1);
    }

    public static void filter(List<Integer> list, Predicate<Integer> predicate) {
        for (Integer item:list) {
            if (predicate.test(item)) {
                System.out.print(item+" ");
            }
        }
        System.out.println();
    }
}
